package com.yanqu.road.dao.impl.activity.goldingotfund;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.goldingotfund.data.GoldIngotFundUserData;
import com.yanqu.road.entity.activity.hsisland.data.HsIslandBuffData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class GoldIngotFundDaoImpl extends TempDataObjectDao<GoldIngotFundUserData> {

    public static String getGoldIngotFundUserDataSql = "select * from `t_u_activity_gold_ingot_fund_user_data` where `activityId`=? and `userId`=?";
    public GoldIngotFundUserData getGoldIngotFundUserData(int activityId,long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGoldIngotFundUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String getRechargeUserDataListSql = "select * from `t_u_activity_gold_ingot_fund_user_data` where `activityId`=? and buyMallKeyIds != '' and buyMallKeyIds != '[]' and buyMallKeyIds is not null";
    public Map<Long, GoldIngotFundUserData> getRechargeUserDataMap(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, GoldIngotFundUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getRechargeUserDataListSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoldIngotFundUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String getGoldIngotFundUserDataListSql = "select * from `t_u_activity_gold_ingot_fund_user_data` where `userId`=?";
    public List<GoldIngotFundUserData> getGoldIngotFundUserDataList(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            List<GoldIngotFundUserData> list = new ArrayList<>();
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getGoldIngotFundUserDataListSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                GoldIngotFundUserData data = parse(rs);
                list.add(data);
            }
            return list;
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
    }

    public static String updateGoldIngotFundUserDataSql = "update `t_u_activity_gold_ingot_fund_user_data` set signDays=?,buyMallKeyIds=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(GoldIngotFundUserData goldIngotFundUserData) {
        goldIngotFundUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(goldIngotFundUserData.getSignDays())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(goldIngotFundUserData.getBuyMallKeyIds())));
        para.put(i++, new DbParameter(Types.INTEGER, goldIngotFundUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goldIngotFundUserData.getUserId()));
        if (getDb().executeNoneQuery(updateGoldIngotFundUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoldIngotFundUserDataSql = "insert into `t_u_activity_gold_ingot_fund_user_data`(activityId,userId,signDays,buyMallKeyIds) values (?,?,?,?)";
    @Override
    protected boolean addData(GoldIngotFundUserData goldIngotFundUserData) {
        goldIngotFundUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, goldIngotFundUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goldIngotFundUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(goldIngotFundUserData.getSignDays())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSONObject.toJSONString(goldIngotFundUserData.getBuyMallKeyIds())));
        if (getDb().executeNoneQuery(insertGoldIngotFundUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoldIngotFundUserData parseData(ResultSet rs) throws SQLException {
        GoldIngotFundUserData goldIngotFundUserData = new GoldIngotFundUserData();
        goldIngotFundUserData.setActivityId(rs.getInt("activityId"));
        goldIngotFundUserData.setUserId(rs.getLong("userId"));
        if (!StringUtils.isNullOrEmpty(rs.getString("signDays"))) {
            goldIngotFundUserData.setSignDays(JSON.parseObject(rs.getString("signDays"), new TypeReference<Set<Integer>>() {}));
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("buyMallKeyIds"))) {
            goldIngotFundUserData.setBuyMallKeyIds(JSON.parseObject(rs.getString("buyMallKeyIds"), new TypeReference<Set<Integer>>() {}));
        }
        return goldIngotFundUserData;
    }
}
